package com.company;

public class SortingHelper {
    private SortingHelper() {
    }

    public static <E extends Comparable<E>> boolean isSorted(E[] arr) {
        for (int i = 1; i < arr.length; i++)
            if (arr[i - 1].compareTo(arr[i]) > 0)
                return false;
        return true;
    }

    /**
     * @param sortname
     * @param arr
     * @param <E>
     */
    public static <E extends Comparable<E>> void sortTest(String sortname, E[] arr) {
        long startTime = System.nanoTime();//纳秒的时间戳
        if (sortname.equals(SortFunctionEum.SelectionSort))
            SelectionSort.sort(arr);
        else if (sortname.equals(InsertionSort.class.toString()))
            InsertionSort.sort(arr);
        else if (sortname.equals("sortSelection"))
            InsertionSort.sortSelection(arr);
        long endTime = System.nanoTime();
        double time = (endTime - startTime) / 1000000000.0;
        if (!SortingHelper.isSorted(arr))
            throw new RuntimeException(sortname + "failed");
        System.out.println(String.format("%s, n=%d: %f s", sortname, arr.length, time));
    }
}
